<template>
    <h1 style="text-align: center;">投资项目修改页面</h1>

    <table>
        <tr>
            <td>投资项目名称</td>
            <td>
                <input type="text" v-model="Info.I_Name">
            </td>
        </tr>

        <tr>
            <td>投资项目总金额</td>
            <td>
                <input type="text" v-model="Info.I_Budget" >
            </td>
        </tr>

        <tr>
            <td colspan="2">
                <input type="button" value="修改" @click="Update">
            </td>
        </tr>

    </table>

</template>

<script setup lang="ts">
import { ref,onMounted } from 'vue';
import axios from 'axios';
import { useRouter,useRoute } from 'vue-router';

const router = useRouter();
const route = useRoute();
const Info = ref({//反填的投资项目信息
    I_Name:'',
    I_Budget:''
})

console.log(route.params.Id);
//生命周期钩子
onMounted (()=>{
    SearInvest();
})
//反填方法
const SearInvest = ()=>{
    axios({
        method:'get',
        url:'http://localhost:63517/api/Invest/SearInvest',
        params:{
            Id:route.params.Id
        }
    })
    .then((res)=>{
        console.log(res.data);
        Info.value = res.data;
    })
    .catch((err)=>{
        console.log(err);
    })
}
//修改方法
const Update = ()=>{

    if(Info.value.I_Name==''){
        alert('投资项目名称不能为空');
        return;
    }

    if(Info.value.I_Budget==''){
        alert('投资项目总金额不能为空');
        return;
    }

    if( Number(Info.value.I_Budget)<=0 || Number(Info.value.I_Budget)>100000000 ){
        alert('投资项目总金额必须大于0且小于1亿元');
        return;
    }

    axios({
        method:'post',
        url:'http://localhost:63517/api/Invest/Update',
        data:{
            I_Id:route.params.Id,
            I_Name:Info.value.I_Name,
            I_Budget:Info.value.I_Budget
        }
    })
    .then((res)=>{
        console.log(res.data);
        if(res.data>0){
            alert('修改成功');
            router.push('/ShowInvestInfo');
        }
        else if(res.data==-1){
            alert('有人投资,无法修改');
            return;
        }
        else{
            alert('修改失败');
            return;
        }
    })
    .catch((err)=>{
        console.log(err);
    })
}

</script>